package org.aphelie.torg.ruler.internal.core.character;

import java.io.PrintWriter;

import org.aphelie.torg.ruler.core.character.ICharacterAttribute;
import org.aphelie.torg.ruler.internal.core.ischema.ISchema;
import org.aphelie.torg.ruler.internal.core.ischema.ISchemaAttribute;
import org.aphelie.torg.ruler.internal.core.ischema.ISchemaElement;
import org.eclipse.core.runtime.CoreException;
import org.w3c.dom.Node;


public class CharacterAttribute extends CharacterObject implements ICharacterAttribute {
    private static final long serialVersionUID = 1L;

    protected String fValue;

    private transient ISchemaAttribute attributeInfo;

    public CharacterAttribute() {
    }

    CharacterAttribute(ICharacterAttribute attribute) {
        setModel(attribute.getModel());
        setParent(attribute.getParent());
        fName = attribute.getName();
        fValue = attribute.getValue();
        this.attributeInfo = ((CharacterAttribute) attribute).getAttributeInfo();
    }

    public Object clone() {
        return new CharacterAttribute(this);
    }

    public boolean equals(Object obj) {
        if (obj == this)
            return true;
        if (obj == null)
            return false;
        if (obj instanceof ICharacterAttribute) {
            ICharacterAttribute target = (ICharacterAttribute) obj;
            if (target.getModel().equals(getModel()))
                return false;
            if (stringEqualWithNull(getName(), target.getName()) && stringEqualWithNull(getValue(), target.getValue()))
                return true;
        }
        return false;
    }

    public ISchemaAttribute getAttributeInfo() {
        if (attributeInfo != null) {
            ISchema schema = attributeInfo.getSchema();
            if (schema.isDisposed()) {
                attributeInfo = null;
            }
        }
        if (attributeInfo == null) {
            CharacterElement element = (CharacterElement) getParent();
            ISchemaElement elementInfo = (ISchemaElement) element.getElementInfo();
            if (elementInfo != null) {
                attributeInfo = elementInfo.getAttribute(getName());
            }
        }
        return attributeInfo;
    }

    public String getValue() {
        return fValue;
    }

    void load(Node node) {
        fName = node.getNodeName();
        fValue = node.getNodeValue();
    }

    void load(String name, String value) {
        fName = name;
        fValue = value;
    }

    public void setAttributeInfo(ISchemaAttribute newAttributeInfo) {
        attributeInfo = newAttributeInfo;
    }

    public void setValue(String newValue) throws CoreException {
        ensureModelEditable();
        String oldValue = fValue;
        fValue = newValue;
        AttributeChangedEvent e = new AttributeChangedEvent(getModel(), getParent(), this, oldValue, newValue);
        fireModelChanged(e);
    }

    public void write(String indent, PrintWriter writer) {
        if (fValue == null)
            return;
        writer.print(indent);
        writer.print(getName() + "=\"" + getWritableString(fValue) + "\""); //$NON-NLS-1$ //$NON-NLS-2$
    }
}
